

<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Debugging with XRay &#8212; LLVM 9 documentation</title>
    <link rel="stylesheet" href="_static/llvm-theme.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
    <script src="_static/jquery.js"></script>
    <script src="_static/underscore.js"></script>
    <script src="_static/doctools.js"></script>
    <script src="_static/language_data.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="XRay Flight Data Recorder Trace Format" href="XRayFDRFormat.html" />
    <link rel="prev" title="XRay Instrumentation" href="XRay.html" />
<style type="text/css">
  table.right { float: right; margin-left: 20px; }
  table.right td { border: 1px solid #ccc; }
</style>

  </head><body>
<div class="logo">
  <a href="index.html">
    <img src="_static/logo.png"
         alt="LLVM Logo" width="250" height="88"/></a>
</div>

    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="XRayFDRFormat.html" title="XRay Flight Data Recorder Trace Format"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="XRay.html" title="XRay Instrumentation"
             accesskey="P">previous</a> |</li>
  <li><a href="http://llvm.org/">LLVM Home</a>&nbsp;|&nbsp;</li>
  <li><a href="index.html">Documentation</a>&raquo;</li>

        <li class="nav-item nav-item-this"><a href="">Debugging with XRay</a></li> 
      </ul>
    </div>


    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <div class="section" id="debugging-with-xray">
<h1>Debugging with XRay<a class="headerlink" href="#debugging-with-xray" title="Permalink to this headline">¶</a></h1>
<p>This document shows an example of how you would go about analyzing applications
built with XRay instrumentation. Here we will attempt to debug <code class="docutils literal notranslate"><span class="pre">llc</span></code>
compiling some sample LLVM IR generated by Clang.</p>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#building-with-xray" id="id1">Building with XRay</a></p></li>
<li><p><a class="reference internal" href="#getting-traces" id="id2">Getting Traces</a></p></li>
<li><p><a class="reference internal" href="#the-llvm-xray-tool" id="id3">The <code class="docutils literal notranslate"><span class="pre">llvm-xray</span></code> Tool</a></p></li>
<li><p><a class="reference internal" href="#controlling-fidelity" id="id4">Controlling Fidelity</a></p>
<ul>
<li><p><a class="reference internal" href="#instruction-threshold" id="id5">Instruction Threshold</a></p></li>
<li><p><a class="reference internal" href="#instrumentation-attributes" id="id6">Instrumentation Attributes</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#the-xray-stack-tool" id="id7">The XRay stack tool</a></p></li>
<li><p><a class="reference internal" href="#flame-graph-generation" id="id8">Flame Graph Generation</a></p></li>
<li><p><a class="reference internal" href="#chrome-trace-viewer-visualization" id="id9">Chrome Trace Viewer Visualization</a></p></li>
<li><p><a class="reference internal" href="#further-exploration" id="id10">Further Exploration</a></p></li>
<li><p><a class="reference internal" href="#next-steps" id="id11">Next Steps</a></p></li>
</ul>
</div>
<div class="section" id="building-with-xray">
<h2><a class="toc-backref" href="#id1">Building with XRay</a><a class="headerlink" href="#building-with-xray" title="Permalink to this headline">¶</a></h2>
<p>To debug an application with XRay instrumentation, we need to build it with a
Clang that supports the <code class="docutils literal notranslate"><span class="pre">-fxray-instrument</span></code> option. See <a class="reference external" href="XRay.html">XRay</a>
for more technical details of how XRay works for background information.</p>
<p>In our example, we need to add <code class="docutils literal notranslate"><span class="pre">-fxray-instrument</span></code> to the list of flags
passed to Clang when building a binary. Note that we need to link with Clang as
well to get the XRay runtime linked in appropriately. For building <code class="docutils literal notranslate"><span class="pre">llc</span></code> with
XRay, we do something similar below for our LLVM build:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ mkdir -p llvm-build &amp;&amp; cd llvm-build
# Assume that the LLVM sources are at ../llvm
$ cmake -GNinja ../llvm -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_C_FLAGS_RELEASE=&quot;-fxray-instrument&quot; -DCMAKE_CXX_FLAGS=&quot;-fxray-instrument&quot; \
# Once this finishes, we should build llc
$ ninja llc
</pre></div>
</div>
<p>To verify that we have an XRay instrumented binary, we can use <code class="docutils literal notranslate"><span class="pre">objdump</span></code> to
look for the <code class="docutils literal notranslate"><span class="pre">xray_instr_map</span></code> section.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ objdump -h -j xray_instr_map ./bin/llc
./bin/llc:     file format elf64-x86-64

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
 14 xray_instr_map 00002fc0  00000000041516c6  00000000041516c6  03d516c6  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
</pre></div>
</div>
</div>
<div class="section" id="getting-traces">
<h2><a class="toc-backref" href="#id2">Getting Traces</a><a class="headerlink" href="#getting-traces" title="Permalink to this headline">¶</a></h2>
<p>By default, XRay does not write out the trace files or patch the application
before main starts. If we run <code class="docutils literal notranslate"><span class="pre">llc</span></code> it should work like a normally built
binary. If we want to get a full trace of the application’s operations (of the
functions we do end up instrumenting with XRay) then we need to enable XRay
at application start. To do this, XRay checks the <code class="docutils literal notranslate"><span class="pre">XRAY_OPTIONS</span></code> environment
variable.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span># The following doesn&#39;t create an XRay trace by default.
$ ./bin/llc input.ll

# We need to set the XRAY_OPTIONS to enable some features.
$ XRAY_OPTIONS=&quot;patch_premain=true xray_mode=xray-basic verbosity=1&quot; ./bin/llc input.ll
==69819==XRay: Log file in &#39;xray-log.llc.m35qPB&#39;
</pre></div>
</div>
<p>At this point we now have an XRay trace we can start analysing.</p>
</div>
<div class="section" id="the-llvm-xray-tool">
<h2><a class="toc-backref" href="#id3">The <code class="docutils literal notranslate"><span class="pre">llvm-xray</span></code> Tool</a><a class="headerlink" href="#the-llvm-xray-tool" title="Permalink to this headline">¶</a></h2>
<p>Having a trace then allows us to do basic accounting of the functions that were
instrumented, and how much time we’re spending in parts of the code. To make
sense of this data, we use the <code class="docutils literal notranslate"><span class="pre">llvm-xray</span></code> tool which has a few subcommands
to help us understand our trace.</p>
<p>One of the things we can do is to get an accounting of the functions that have
been instrumented. We can see an example accounting with <code class="docutils literal notranslate"><span class="pre">llvm-xray</span> <span class="pre">account</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ llvm-xray account xray-log.llc.m35qPB -top=10 -sort=sum -sortorder=dsc -instr_map ./bin/llc
Functions with latencies: 29
   funcid      count [      min,       med,       90p,       99p,       max]       sum  function
      187        360 [ 0.000000,  0.000001,  0.000014,  0.000032,  0.000075]  0.001596  LLLexer.cpp:446:0: llvm::LLLexer::LexIdentifier()
       85        130 [ 0.000000,  0.000000,  0.000018,  0.000023,  0.000156]  0.000799  X86ISelDAGToDAG.cpp:1984:0: (anonymous namespace)::X86DAGToDAGISel::Select(llvm::SDNode*)
      138        130 [ 0.000000,  0.000000,  0.000017,  0.000155,  0.000155]  0.000774  SelectionDAGISel.cpp:2963:0: llvm::SelectionDAGISel::SelectCodeCommon(llvm::SDNode*, unsigned char const*, unsigned int)
      188        103 [ 0.000000,  0.000000,  0.000003,  0.000123,  0.000214]  0.000737  LLParser.cpp:2692:0: llvm::LLParser::ParseValID(llvm::ValID&amp;, llvm::LLParser::PerFunctionState*)
       88          1 [ 0.000562,  0.000562,  0.000562,  0.000562,  0.000562]  0.000562  X86ISelLowering.cpp:83:0: llvm::X86TargetLowering::X86TargetLowering(llvm::X86TargetMachine const&amp;, llvm::X86Subtarget const&amp;)
      125        102 [ 0.000001,  0.000003,  0.000010,  0.000017,  0.000049]  0.000471  Verifier.cpp:3714:0: (anonymous namespace)::Verifier::visitInstruction(llvm::Instruction&amp;)
       90          8 [ 0.000023,  0.000035,  0.000106,  0.000106,  0.000106]  0.000342  X86ISelLowering.cpp:3363:0: llvm::X86TargetLowering::LowerCall(llvm::TargetLowering::CallLoweringInfo&amp;, llvm::SmallVectorImpl&lt;llvm::SDValue&gt;&amp;) const
      124         32 [ 0.000003,  0.000007,  0.000016,  0.000041,  0.000041]  0.000310  Verifier.cpp:1967:0: (anonymous namespace)::Verifier::visitFunction(llvm::Function const&amp;)
      123          1 [ 0.000302,  0.000302,  0.000302,  0.000302,  0.000302]  0.000302  LLVMContextImpl.cpp:54:0: llvm::LLVMContextImpl::~LLVMContextImpl()
      139         46 [ 0.000000,  0.000002,  0.000006,  0.000008,  0.000019]  0.000138  TargetLowering.cpp:506:0: llvm::TargetLowering::SimplifyDemandedBits(llvm::SDValue, llvm::APInt const&amp;, llvm::APInt&amp;, llvm::APInt&amp;, llvm::TargetLowering::TargetLoweringOpt&amp;, unsigned int, bool) const
</pre></div>
</div>
<p>This shows us that for our input file, <code class="docutils literal notranslate"><span class="pre">llc</span></code> spent the most cumulative time
in the lexer (a total of 1 millisecond). If we wanted for example to work with
this data in a spreadsheet, we can output the results as CSV using the
<code class="docutils literal notranslate"><span class="pre">-format=csv</span></code> option to the command for further analysis.</p>
<p>If we want to get a textual representation of the raw trace we can use the
<code class="docutils literal notranslate"><span class="pre">llvm-xray</span> <span class="pre">convert</span></code> tool to get YAML output. The first few lines of that
output for an example trace would look like the following:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ llvm-xray convert -f yaml -symbolize -instr_map=./bin/llc xray-log.llc.m35qPB
---
header:
  version:         1
  type:            0
  constant-tsc:    true
  nonstop-tsc:     true
  cycle-frequency: 2601000000
records:
  - { type: 0, func-id: 110, function: __cxx_global_var_init.8, cpu: 37, thread: 69819, kind: function-enter, tsc: 5434426023268520 }
  - { type: 0, func-id: 110, function: __cxx_global_var_init.8, cpu: 37, thread: 69819, kind: function-exit, tsc: 5434426023523052 }
  - { type: 0, func-id: 164, function: __cxx_global_var_init, cpu: 37, thread: 69819, kind: function-enter, tsc: 5434426029925386 }
  - { type: 0, func-id: 164, function: __cxx_global_var_init, cpu: 37, thread: 69819, kind: function-exit, tsc: 5434426030031128 }
  - { type: 0, func-id: 142, function: &#39;(anonymous namespace)::CommandLineParser::ParseCommandLineOptions(int, char const* const*, llvm::StringRef, llvm::raw_ostream*)&#39;, cpu: 37, thread: 69819, kind: function-enter, tsc: 5434426046951388 }
  - { type: 0, func-id: 142, function: &#39;(anonymous namespace)::CommandLineParser::ParseCommandLineOptions(int, char const* const*, llvm::StringRef, llvm::raw_ostream*)&#39;, cpu: 37, thread: 69819, kind: function-exit, tsc: 5434426047282020 }
  - { type: 0, func-id: 187, function: &#39;llvm::LLLexer::LexIdentifier()&#39;, cpu: 37, thread: 69819, kind: function-enter, tsc: 5434426047857332 }
  - { type: 0, func-id: 187, function: &#39;llvm::LLLexer::LexIdentifier()&#39;, cpu: 37, thread: 69819, kind: function-exit, tsc: 5434426047984152 }
  - { type: 0, func-id: 187, function: &#39;llvm::LLLexer::LexIdentifier()&#39;, cpu: 37, thread: 69819, kind: function-enter, tsc: 5434426048036584 }
  - { type: 0, func-id: 187, function: &#39;llvm::LLLexer::LexIdentifier()&#39;, cpu: 37, thread: 69819, kind: function-exit, tsc: 5434426048042292 }
  - { type: 0, func-id: 187, function: &#39;llvm::LLLexer::LexIdentifier()&#39;, cpu: 37, thread: 69819, kind: function-enter, tsc: 5434426048055056 }
  - { type: 0, func-id: 187, function: &#39;llvm::LLLexer::LexIdentifier()&#39;, cpu: 37, thread: 69819, kind: function-exit, tsc: 5434426048067316 }
</pre></div>
</div>
</div>
<div class="section" id="controlling-fidelity">
<h2><a class="toc-backref" href="#id4">Controlling Fidelity</a><a class="headerlink" href="#controlling-fidelity" title="Permalink to this headline">¶</a></h2>
<p>So far in our examples, we haven’t been getting full coverage of the functions
we have in the binary. To get that, we need to modify the compiler flags so
that we can instrument more (if not all) the functions we have in the binary.
We have two options for doing that, and we explore both of these below.</p>
<div class="section" id="instruction-threshold">
<h3><a class="toc-backref" href="#id5">Instruction Threshold</a><a class="headerlink" href="#instruction-threshold" title="Permalink to this headline">¶</a></h3>
<p>The first “blunt” way of doing this is by setting the minimum threshold for
function bodies to 1. We can do that with the
<code class="docutils literal notranslate"><span class="pre">-fxray-instruction-threshold=N</span></code> flag when building our binary. We rebuild
<code class="docutils literal notranslate"><span class="pre">llc</span></code> with this option and observe the results:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ rm CMakeCache.txt
$ cmake -GNinja ../llvm -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_C_FLAGS_RELEASE=&quot;-fxray-instrument -fxray-instruction-threshold=1&quot; \
    -DCMAKE_CXX_FLAGS=&quot;-fxray-instrument -fxray-instruction-threshold=1&quot;
$ ninja llc
$ XRAY_OPTIONS=&quot;patch_premain=true&quot; ./bin/llc input.ll
==69819==XRay: Log file in &#39;xray-log.llc.5rqxkU&#39;

$ llvm-xray account xray-log.llc.5rqxkU -top=10 -sort=sum -sortorder=dsc -instr_map ./bin/llc
Functions with latencies: 36652
 funcid      count [      min,       med,       90p,       99p,       max]       sum  function
     75          1 [ 0.672368,  0.672368,  0.672368,  0.672368,  0.672368]  0.672368  llc.cpp:271:0: main
     78          1 [ 0.626455,  0.626455,  0.626455,  0.626455,  0.626455]  0.626455  llc.cpp:381:0: compileModule(char**, llvm::LLVMContext&amp;)
 139617          1 [ 0.472618,  0.472618,  0.472618,  0.472618,  0.472618]  0.472618  LegacyPassManager.cpp:1723:0: llvm::legacy::PassManager::run(llvm::Module&amp;)
 139610          1 [ 0.472618,  0.472618,  0.472618,  0.472618,  0.472618]  0.472618  LegacyPassManager.cpp:1681:0: llvm::legacy::PassManagerImpl::run(llvm::Module&amp;)
 139612          1 [ 0.470948,  0.470948,  0.470948,  0.470948,  0.470948]  0.470948  LegacyPassManager.cpp:1564:0: (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&amp;)
 139607          2 [ 0.147345,  0.315994,  0.315994,  0.315994,  0.315994]  0.463340  LegacyPassManager.cpp:1530:0: llvm::FPPassManager::runOnModule(llvm::Module&amp;)
 139605         21 [ 0.000002,  0.000002,  0.102593,  0.213336,  0.213336]  0.463331  LegacyPassManager.cpp:1491:0: llvm::FPPassManager::runOnFunction(llvm::Function&amp;)
 139563      26096 [ 0.000002,  0.000002,  0.000037,  0.000063,  0.000215]  0.225708  LegacyPassManager.cpp:1083:0: llvm::PMDataManager::findAnalysisPass(void const*, bool)
 108055        188 [ 0.000002,  0.000120,  0.001375,  0.004523,  0.062624]  0.159279  MachineFunctionPass.cpp:38:0: llvm::MachineFunctionPass::runOnFunction(llvm::Function&amp;)
  62635         22 [ 0.000041,  0.000046,  0.000050,  0.126744,  0.126744]  0.127715  X86TargetMachine.cpp:242:0: llvm::X86TargetMachine::getSubtargetImpl(llvm::Function const&amp;) const
</pre></div>
</div>
</div>
<div class="section" id="instrumentation-attributes">
<h3><a class="toc-backref" href="#id6">Instrumentation Attributes</a><a class="headerlink" href="#instrumentation-attributes" title="Permalink to this headline">¶</a></h3>
<p>The other way is to use configuration files for selecting which functions
should always be instrumented by the compiler. This gives us a way of ensuring
that certain functions are either always or never instrumented by not having to
add the attribute to the source.</p>
<p>To use this feature, you can define one file for the functions to always
instrument, and another for functions to never instrument. The format of these
files are exactly the same as the SanitizerLists files that control similar
things for the sanitizer implementations. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># xray-attr-list.txt</span>
<span class="c1"># always instrument functions that match the following filters:</span>
<span class="p">[</span><span class="n">always</span><span class="p">]</span>
<span class="n">fun</span><span class="p">:</span><span class="n">main</span>

<span class="c1"># never instrument functions that match the following filters:</span>
<span class="p">[</span><span class="n">never</span><span class="p">]</span>
<span class="n">fun</span><span class="p">:</span><span class="n">__cxx_</span><span class="o">*</span>
</pre></div>
</div>
<p>Given the file above we can re-build by providing it to the
<code class="docutils literal notranslate"><span class="pre">-fxray-attr-list=</span></code> flag to clang. You can have multiple files, each defining
different sets of attribute sets, to be combined into a single list by clang.</p>
</div>
</div>
<div class="section" id="the-xray-stack-tool">
<h2><a class="toc-backref" href="#id7">The XRay stack tool</a><a class="headerlink" href="#the-xray-stack-tool" title="Permalink to this headline">¶</a></h2>
<p>Given a trace, and optionally an instrumentation map, the <code class="docutils literal notranslate"><span class="pre">llvm-xray</span> <span class="pre">stack</span></code>
command can be used to analyze a call stack graph constructed from the function
call timeline.</p>
<p>The way to use the command is to output the top stacks by call count and time spent.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ llvm-xray stack xray-log.llc.5rqxkU -instr_map ./bin/llc

Unique Stacks: 3069
Top 10 Stacks by leaf sum:

Sum: 9633790
lvl   function                                                            count              sum
#0    main                                                                    1         58421550
#1    compileModule(char**, llvm::LLVMContext&amp;)                               1         51440360
#2    llvm::legacy::PassManagerImpl::run(llvm::Module&amp;)                       1         40535375
#3    llvm::FPPassManager::runOnModule(llvm::Module&amp;)                         2         39337525
#4    llvm::FPPassManager::runOnFunction(llvm::Function&amp;)                     6         39331465
#5    llvm::PMDataManager::verifyPreservedAnalysis(llvm::Pass*)             399         16628590
#6    llvm::PMTopLevelManager::findAnalysisPass(void const*)               4584         15155600
#7    llvm::PMDataManager::findAnalysisPass(void const*, bool)            32088          9633790

..etc..
</pre></div>
</div>
<p>In the default mode, identical stacks on different threads are independently
aggregated. In a multithreaded program, you may end up having identical call
stacks fill your list of top calls.</p>
<p>To address this, you may specify the <code class="docutils literal notranslate"><span class="pre">-aggregate-threads</span></code> or
<code class="docutils literal notranslate"><span class="pre">-per-thread-stacks</span></code> flags. <code class="docutils literal notranslate"><span class="pre">-per-thread-stacks</span></code> treats the thread id as an
implicit root in each call stack tree, while <code class="docutils literal notranslate"><span class="pre">-aggregate-threads</span></code> combines
identical stacks from all threads.</p>
</div>
<div class="section" id="flame-graph-generation">
<h2><a class="toc-backref" href="#id8">Flame Graph Generation</a><a class="headerlink" href="#flame-graph-generation" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">llvm-xray</span> <span class="pre">stack</span></code> tool may also be used to generate flamegraphs for
visualizing your instrumented invocations. The tool does not generate the graphs
themselves, but instead generates a format that can be used with Brendan Gregg’s
FlameGraph tool, currently available on <a class="reference external" href="https://github.com/brendangregg/FlameGraph">github</a>.</p>
<p>To generate output for a flamegraph, a few more options are necessary.</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">-all-stacks</span></code> - Emits all of the stacks.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">-stack-format</span></code> - Choose the flamegraph output format ‘flame’.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">-aggregation-type</span></code> - Choose the metric to graph.</p></li>
</ul>
<p>You may pipe the command output directly to the flamegraph tool to obtain an
svg file.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$llvm-xray stack xray-log.llc.5rqxkU -instr_map ./bin/llc -stack-format=flame -aggregation-type=time -all-stacks | \
/path/to/FlameGraph/flamegraph.pl &gt; flamegraph.svg
</pre></div>
</div>
<p>If you open the svg in a browser, mouse events allow exploring the call stacks.</p>
</div>
<div class="section" id="chrome-trace-viewer-visualization">
<h2><a class="toc-backref" href="#id9">Chrome Trace Viewer Visualization</a><a class="headerlink" href="#chrome-trace-viewer-visualization" title="Permalink to this headline">¶</a></h2>
<p>We can also generate a trace which can be loaded by the Chrome Trace Viewer
from the same generated trace:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ llvm-xray convert -symbolize -instr_map=./bin/llc \
  -output-format=trace_event xray-log.llc.5rqxkU \
    | gzip &gt; llc-trace.txt.gz
</pre></div>
</div>
<p>From a Chrome browser, navigating to <code class="docutils literal notranslate"><span class="pre">chrome:///tracing</span></code> allows us to load
the <code class="docutils literal notranslate"><span class="pre">sample-trace.txt.gz</span></code> file to visualize the execution trace.</p>
</div>
<div class="section" id="further-exploration">
<h2><a class="toc-backref" href="#id10">Further Exploration</a><a class="headerlink" href="#further-exploration" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">llvm-xray</span></code> tool has a few other subcommands that are in various stages
of being developed. One interesting subcommand that can highlight a few
interesting things is the <code class="docutils literal notranslate"><span class="pre">graph</span></code> subcommand. Given for example the following
toy program that we build with XRay instrumentation, we can see how the
generated graph may be a helpful indicator of where time is being spent for the
application.</p>
<div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="c1">// sample.cc</span>
<span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;thread&gt;</span><span class="cp"></span>

<span class="p">[[</span><span class="n">clang</span><span class="o">::</span><span class="n">xray_always_instrument</span><span class="p">]]</span> <span class="kt">void</span> <span class="n">f</span><span class="p">()</span> <span class="p">{</span>
  <span class="n">std</span><span class="o">::</span><span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="sc">&#39;.&#39;</span><span class="p">;</span>
<span class="p">}</span>

<span class="p">[[</span><span class="n">clang</span><span class="o">::</span><span class="n">xray_always_instrument</span><span class="p">]]</span> <span class="kt">void</span> <span class="n">g</span><span class="p">()</span> <span class="p">{</span>
  <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">1</span> <span class="o">&lt;&lt;</span> <span class="mi">10</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">std</span><span class="o">::</span><span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="sc">&#39;-&#39;</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="n">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">*</span> <span class="n">argv</span><span class="p">[])</span> <span class="p">{</span>
  <span class="n">std</span><span class="o">::</span><span class="kr">thread</span> <span class="n">t1</span><span class="p">([]</span> <span class="p">{</span>
    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">1</span> <span class="o">&lt;&lt;</span> <span class="mi">10</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span>
      <span class="n">f</span><span class="p">();</span>
  <span class="p">});</span>
  <span class="n">std</span><span class="o">::</span><span class="kr">thread</span> <span class="n">t2</span><span class="p">([]</span> <span class="p">{</span>
    <span class="n">g</span><span class="p">();</span>
  <span class="p">});</span>
  <span class="n">t1</span><span class="p">.</span><span class="n">join</span><span class="p">();</span>
  <span class="n">t2</span><span class="p">.</span><span class="n">join</span><span class="p">();</span>
  <span class="n">std</span><span class="o">::</span><span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="sc">&#39;\n&#39;</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>We then build the above with XRay instrumentation:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ clang++ -o sample -O3 sample.cc -std=c++11 -fxray-instrument -fxray-instruction-threshold=1
$ XRAY_OPTIONS=&quot;patch_premain=true xray_mode=xray-basic&quot; ./sample
</pre></div>
</div>
<p>We can then explore the graph rendering of the trace generated by this sample
application. We assume you have the graphviz toosl available in your system,
including both <code class="docutils literal notranslate"><span class="pre">unflatten</span></code> and <code class="docutils literal notranslate"><span class="pre">dot</span></code>. If you prefer rendering or exploring
the graph using another tool, then that should be feasible as well. <code class="docutils literal notranslate"><span class="pre">llvm-xray</span>
<span class="pre">graph</span></code> will create DOT format graphs which should be usable in most graph
rendering applications. One example invocation of the <code class="docutils literal notranslate"><span class="pre">llvm-xray</span> <span class="pre">graph</span></code>
command should yield some interesting insights to the workings of C++
applications:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ llvm-xray graph xray-log.sample.* -m sample -color-edges=sum -edge-label=sum \
    | unflatten -f -l10 | dot -Tsvg -o sample.svg
</pre></div>
</div>
</div>
<div class="section" id="next-steps">
<h2><a class="toc-backref" href="#id11">Next Steps</a><a class="headerlink" href="#next-steps" title="Permalink to this headline">¶</a></h2>
<p>If you have some interesting analyses you’d like to implement as part of the
llvm-xray tool, please feel free to propose them on the llvm-dev&#64; mailing list.
The following are some ideas to inspire you in getting involved and potentially
making things better.</p>
<blockquote>
<div><ul class="simple">
<li><p>Implement a query/filtering library that allows for finding patterns in the
XRay traces.</p></li>
<li><p>Collecting function call stacks and how often they’re encountered in the
XRay trace.</p></li>
</ul>
</div></blockquote>
</div>
</div>


            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="XRayFDRFormat.html" title="XRay Flight Data Recorder Trace Format"
             >next</a> |</li>
        <li class="right" >
          <a href="XRay.html" title="XRay Instrumentation"
             >previous</a> |</li>
  <li><a href="http://llvm.org/">LLVM Home</a>&nbsp;|&nbsp;</li>
  <li><a href="index.html">Documentation</a>&raquo;</li>

        <li class="nav-item nav-item-this"><a href="">Debugging with XRay</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2003-2020, LLVM Project.
      Last updated on 2020-09-20.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.2.1.
    </div>
  </body>
</html>